package control;

import com.toedter.components.JSpinField;
import control.bean.ControlBeanConstant;
import control.bean.ControlPropertiesBean;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created with IntelliJ IDEA.
 * User: Chen Deshun
 * Date: 2/19/13
 * Time: 10:25 PM
 * To change this template use File | Settings | File Templates.
 */
public class TimePicker extends Control {

    private JSpinField hour;
    private JSpinField min;

    public TimePicker() {
        fieldType = ControlBeanConstant.TIMEPICKER;
        buildControlPanel();
        buildPropertiesPanel();
    }

    @Override
    public Component buildControlPanel() {
        controlPanel = new JPanel(new GridBagLayout());
        controlPanel.setOpaque(false);

        GridBagConstraints constraints = new GridBagConstraints();

        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.ipadx = 130;
        constraints.insets = new Insets(5, 5, 5, 5);
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;

        label = new JTextField();
        controlPanel.add(label, constraints);

        constraints.gridx = 1;
        constraints.ipadx = 0;
        constraints.insets = new Insets(5, 0, 0, 5);

        hour = new JSpinField(1, 12);
        hour.adjustWidthToMaximumValue();
        controlPanel.add(hour, constraints);

        constraints.gridx = 2;

        min = new JSpinField(0, 59);
        min.adjustWidthToMaximumValue();
        controlPanel.add(min, constraints);

        controlPanel.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                formArea.showProperties(propertiesPanel());
            }

            @Override
            public void focusLost(FocusEvent e) {
                formArea.hideProperties();
            }
        });

        controlPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                ((JPanel) e.getSource()).requestFocus();
            }
        });

        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                formArea.showProperties(propertiesPanel());
            }
        });

        return controlPanel;
    }

    @Override
    public Component buildPropertiesPanel() {
        propertiesPanel = new JPanel(new GridBagLayout());

        fieldProperties = new JLabel("Field Properties");

        controlLabel = new JLabel("Control Name");
        controlName = new JTextField();

        hint = new JCheckBox("Hint");
        required = new JCheckBox("Required");
        readOnly = new JCheckBox("Read Only");

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 1;
//        constraints.ipadx = 130;
        constraints.ipady = 14;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(fieldProperties, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.ipadx = 0;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(controlLabel, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        //constraints.gridheight = 2;
//        constraints.ipadx = 114;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(5, 0, 0, 0);
        propertiesPanel.add(controlName, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 3;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(hint, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 4;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(1, 0, 0, 0);
        propertiesPanel.add(required, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 5;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(1, 0, 0, 0);
        propertiesPanel.add(readOnly, constraints);

        Component strut = Box.createHorizontalStrut(160);
        constraints.gridy = 6;
        propertiesPanel.add(strut, constraints);

        return propertiesPanel;
    }

    public ControlPropertiesBean exportData() {
        super.exportData();

        try {
            controlPropertiesBean.addProperty(ControlBeanConstant.HOUR, Integer.toString(hour.getValue()));
            controlPropertiesBean.addProperty(ControlBeanConstant.MINUTE, Integer.toString(min.getValue()));
        } catch ( Exception e ) {
            System.out.println("Drop down list not set.");
        }

        return controlPropertiesBean;
    }

    public void importData(ControlPropertiesBean controlPropertiesBean) {
        try {
            super.importData(controlPropertiesBean);
            hour.setValue(Integer.parseInt(controlPropertiesBean.getProperty(ControlBeanConstant.HOUR)));
            min.setValue(Integer.parseInt(controlPropertiesBean.getProperty(ControlBeanConstant.MINUTE)));
        } catch ( Exception e ) {
            System.out.println("Loading failed at timepicker");
        }
    }
}
